package com.luom.springbootinit.controller;

import com.github.pagehelper.PageInfo;
import com.luom.springbootinit.annotation.AuthCheck;
import com.luom.springbootinit.common.BaseResponse;
import com.luom.springbootinit.common.DeleteRequest;
import com.luom.springbootinit.common.ErrorCode;
import com.luom.springbootinit.common.ResultUtils;
import com.luom.springbootinit.constant.UserConstant;
import com.luom.springbootinit.exception.BusinessException;
import com.luom.springbootinit.exception.ThrowUtils;
import com.luom.springbootinit.model.dto.borrowRecord.BorrowRecordAddRequest;
import com.luom.springbootinit.model.dto.borrowRecord.BorrowRecordQueryRequest;
import com.luom.springbootinit.model.dto.borrowRecord.BorrowRecordUpdateRequest;
import com.luom.springbootinit.model.entity.BorrowRecord;
import com.luom.springbootinit.model.entity.User;
import com.luom.springbootinit.model.vo.BorrowRecordVO;
import com.luom.springbootinit.service.BookService;
import com.luom.springbootinit.service.BorrowRecordService;
import com.luom.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 借阅信息接口
 */
@RestController
@RequestMapping("/borrowRecord")
@Slf4j
public class BorrowRecordController {

    @Resource
    private BorrowRecordService borrowRecordService;

    @Resource
    private UserService userService;

    @Resource
    private BookService bookService;

    // region 增删改查

    /**
     * 创建借阅信息
     *
     * @param borrowRecordAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @Transactional
    public BaseResponse<Long> addBorrowRecord(@RequestBody BorrowRecordAddRequest borrowRecordAddRequest, HttpServletRequest request) {
        // 判断参数是否为空
        ThrowUtils.throwIf(borrowRecordAddRequest == null, ErrorCode.PARAMS_ERROR);

        long newId=borrowRecordService.addBorrowRecord(borrowRecordAddRequest,request);
        return ResultUtils.success(newId);
    }

    /**
     * 删除借阅信息
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteBorrowRecord(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        int id = deleteRequest.getId();
        // 判断是否存在
        BorrowRecord oldBorrowRecord = borrowRecordService.getById(id);
        ThrowUtils.throwIf(oldBorrowRecord == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = borrowRecordService.removeById(id);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 更新借阅信息（仅管理员可用）
     *
     * @param borrowRecordUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateBorrowRecord(@RequestBody BorrowRecordUpdateRequest borrowRecordUpdateRequest) {
        if (borrowRecordUpdateRequest == null || borrowRecordUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result=borrowRecordService.updateRecord(borrowRecordUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取借阅信息（封装类）
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<BorrowRecordVO> getBorrowRecordVOById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        BorrowRecord borrowRecord = borrowRecordService.getById(id);
        ThrowUtils.throwIf(borrowRecord == null, ErrorCode.NOT_FOUND_ERROR);
        // 获取封装类
        return ResultUtils.success(borrowRecordService.getBorrowRecordVO(borrowRecord, request));
    }

    /**
     * 分页获取当前登录用户创建的借阅信息列表
     *
     * @param borrowRecordQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<PageInfo<BorrowRecordVO>> listMyBorrowRecordVOByPage(@RequestBody BorrowRecordQueryRequest borrowRecordQueryRequest,
                                                                         HttpServletRequest request) {
        ThrowUtils.throwIf(borrowRecordQueryRequest == null, ErrorCode.PARAMS_ERROR);

        long size = borrowRecordQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        PageInfo<BorrowRecordVO> page=borrowRecordService.getBorrowRecordVOPage(true,borrowRecordQueryRequest,request);
        // 获取封装类
        return ResultUtils.success(page);
    }

    /**
     * 分页获取借阅信息列表（封装类）
     *
     * @param borrowRecordQueryRequest
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<PageInfo<BorrowRecordVO>> listBorrowRecordVOByPage(@RequestBody BorrowRecordQueryRequest borrowRecordQueryRequest,
                                                                               HttpServletRequest request) {
        if (borrowRecordQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求为空");
        }
        long size = borrowRecordQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);

        PageInfo<BorrowRecordVO> page=borrowRecordService.getBorrowRecordVOPage(false,borrowRecordQueryRequest,request);
        // 获取封装类
        return ResultUtils.success(page);
    }

    // endregion

    /**
     * 分页获取借阅信息列表（仅管理员可用）
     *
     * @param borrowRecordQueryRequest
     * @return
     */
    /*@PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<BorrowRecord>> listBorrowRecordByPage(@RequestBody BorrowRecordQueryRequest borrowRecordQueryRequest) {
        long current = borrowRecordQueryRequest.getCurrent();
        long size = borrowRecordQueryRequest.getPageSize();
        // 查询数据库
        Page<BorrowRecord> borrowRecordPage = borrowRecordService.page(new Page<>(current, size),
                borrowRecordService.getQueryWrapper(borrowRecordQueryRequest));
        return ResultUtils.success(borrowRecordPage);
    }*/

    /**
     * 分页获取借阅信息列表（封装类）
     *
     * @param borrowRecordQueryRequest
     * @param request
     * @return
     */
    /*@PostMapping("/list/page/vo")
    public BaseResponse<Page<BorrowRecordVO>> listBorrowRecordVOByPage(@RequestBody BorrowRecordQueryRequest borrowRecordQueryRequest,
                                                                       HttpServletRequest request) {
        if (borrowRecordQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求为空");
        }

        long current = borrowRecordQueryRequest.getCurrent();
        long size = borrowRecordQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        //填充用户id列表和图书id列表
        borrowRecordQueryRequest=borrowRecordService.addQueryParams(borrowRecordQueryRequest);
        // 查询数据库
        QueryWrapper<BorrowRecord> queryWrapper = borrowRecordService.getQueryWrapper(borrowRecordQueryRequest);

        Page<BorrowRecord> borrowRecordPage = borrowRecordService.page(new Page<>(current, size),queryWrapper);
        // 获取封装类
        return ResultUtils.success(borrowRecordService.getBorrowRecordVOPage(borrowRecordPage, request));
    }*/
}

